package cn.com.taurus.dispose;

import cn.com.taurus.biz.AutomaticPlanMessageBiz;
import cn.com.taurus.biz.ClientAccountBiz;
import cn.com.taurus.biz.DealMessageBiz;
import cn.com.taurus.biz.FundBiz;
import cn.com.taurus.controller.user.LoginRegisterController;
import cn.com.taurus.entity.AutomaticPlanMessage;
import cn.com.taurus.entity.ClientAccount;
import cn.com.taurus.entity.DealMessage;
import cn.com.taurus.entity.Fund;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 要做的事
 * Job
 */
public class DateTimeJob extends QuartzJobBean {

    @Autowired
    AutomaticPlanMessageBiz automaticPlanMessageBiz;
    @Autowired
    ClientAccountBiz clientAccountBiz;
    @Autowired
    DealMessageBiz dealMessageBiz;
    @Autowired
    LoginRegisterController loginRegisterController;
    @Autowired
    FundBiz fundBiz;

    //工作
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取当天的日期
        Date dateNow = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
        //获取当天的天数
        String dayNow = sdf.format(dateNow.getTime());
        //每周一到周五的上午9:15进行定投操作
        //查询定投表
        List<AutomaticPlanMessage> automaticPlanMessages = automaticPlanMessageBiz.queryAllAutomaticPlanMessage();
        for (AutomaticPlanMessage automaticPlanMessage:automaticPlanMessages) {
            //获取定投设置 后面的什么天数定投
            String day = automaticPlanMessage.getApmCycle().split(",")[1];
            day=Integer.parseInt(day) < 10 ? 0+day : day;
            System.out.println(dayNow+" "+day);
            //如果今天等于定投日期
            if (dayNow.equals(day)){
                //根据automaticPlanMessage获取caId
                Integer caId = automaticPlanMessage.getCaId();
                //计算手续费 定投金额 * 费率 / 100
                Fund fund = fundBiz.selectFundByFid(automaticPlanMessage.getfId());
                Double serviceCharge = automaticPlanMessage.getApmMoney() * fund.getAfterRate() / 100;
                //新建申购对象
                DealMessage dealMessage = new DealMessage
                        (dealMessageBiz.selectDmIdValue(),caId,automaticPlanMessage.getfId(),automaticPlanMessage.getApmMoney(),sdf2.format(dateNow.getTime()),serviceCharge);
                //添加对象进数据库
                dealMessageBiz.buyApplication(dealMessage);
                System.err.printf("WTF?????");
                //修改最后定投时间
                automaticPlanMessage.setStartTime(sdf2.format(dateNow.getTime()));

                //查询出这个定投的客户账户根据caId查询cId查
                ClientAccount clientAccount = clientAccountBiz.selectAllPurse(clientAccountBiz.selectCIdByCaId(caId));
                //修改 公募基金 储蓄罐活期
                clientAccount.setPublicFunds(clientAccount.getPublicFunds() + automaticPlanMessage.getApmMoney());
                clientAccount.setMoneyBox(clientAccount.getMoneyBox() - automaticPlanMessage.getApmMoney());
                //修改存入数据库
                clientAccountBiz.clientAccountUpdate(clientAccount);

                automaticPlanMessageBiz.updateAutomaticPlanMessageByObject(automaticPlanMessage);
            }
        }
    }
}
